package com.bjsxt.controller;


import com.bjsxt.domain.User;
import com.bjsxt.model.R;

import com.bjsxt.model.*;
import com.bjsxt.service.LoginService;
import com.bjsxt.service.UserService;
import com.bjsxt.service.impl.LoginServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "用户登录")
public class LoginController {
    @Autowired
  private LoginService loginService;
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R<LoginUser> login(@RequestBody LoginForm loginForm){
        LoginUser loginUser = loginService.login(loginForm);
        return R.ok(loginUser);
    }

    @GetMapping("/current/info")
    public R<User>currentUserInfo(){
        String idStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        User user = userService.getById(Long.valueOf(idStr));
        user.setPassword("****");
        user.setPaypassword("****");
                user.setAccessKeyId("****");
                user.setAccessKeySecret("****");
             return R.ok(user);


    }

    @GetMapping("/authAccount")
    public R identifyCheck(@RequestBody UserAuthForm userAuthForm){
        String idstr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();

        boolean isOk = userService.identifyVerfiy(Long.valueOf(idstr),userAuthForm);
        if(isOk){
            return R.ok();
        }
        return  R.fail();

    }



}
